<?php

/**
 * 
 * @author lfYang
 * <p>Group存在4列，分别是<b>id</b>, <b>name</b>, <b>info</b>, <b>ts_created</b></p>
 */
class GroupModel extends BaseModel {
	
	/**
	 *
	 * @var Logger
	 */
	private $_logger;
	
	/**
	 * <h2>构造函数，调用父类构造函数</h2>
	 */
	public function __construct() {
		parent::__construct ();
		Logger::configure ( ROOT_PATH . 'config/log_config.xml' );
		$this->_logger = Logger::getLogger ( __CLASS__ );
	}
	
	/**
	 * <h2>根据组名返回组ID</h2>
	 *
	 * @param string $sName
	 *        	用户组名称
	 * @return int 组ID
	 */
	public function getIdByName($sName) {
		if (empty ( $sName ) || is_null ( $this->_conn )) {
			return false;
		}
		
		$query = "SELECT id FROM groups WHERE name=:name LIMIT 1";
		$param = array (
				"name" => $sName 
		);
		$map [] = array (
				"name",
				"string" 
		);
		$table = $this->_conn->select ( $query, $param, $map );
		if (count ( $table )) {
			return ( int ) $table [0] ["id"];
		}
		return false;
	}
	
	/**
	 * <h2>根据组ID获取组名</h2>
	 * <p>如果存在id为$id的组，则返回改组的组名，否则返回<b>null</b></p>
	 *
	 * @param int $id        	
	 * @return string
	 */
	public function getNameById($id) {
		$query = 'select name from groups where id=:id';
		// @formatter:off
		$param['id']=$id;
		$map[] = array('id','int');
		// @formatter:on
		$resultSet = $this->_conn->select ( $query, $param, $map );
		if (count ( $resultSet )) {
			return $resultSet [0] ['name'];
		}
		return null;
	}
	
	/**
	 * <h2>获取一个Group</h2>
	 * <p>如果存在id等于$id的组，则返回一个按列明映射的CMAP对象，否则返回<b>null</b></p>
	 *
	 * @param int $id
	 *        	组ID
	 * @return CMAP
	 */
	public function getGroup($id) {
		$query = 'select id, name, info, ts_created from groups where id=:id';
		// @formatter:off
		$param['id']=$id;
		$map[] = array('id','int');
		// @formatter:on
		$resultSet = $this->_conn->select ( $query, $param, $map );
		if (count ( $resultSet )) {
			return new CMAP ( $resultSet [0] );
		}
		return null;
	}
	
	/**
	 * <h2>返回分组的数量</h2>
	 *
	 * @return number
	 */
	public function getTotalCount() {
		$query = "select count(id) as total from groups";
		$conn = DBHelper::getDB ( false );
		$resultSet = $conn->select ( $query );
		if (count ( $resultSet )) {
			return $resultSet [0] ['total'];
		}
		return 0;
	}
	
	/**
	 *
	 * @param int $pageNo
	 *        	页码
	 * @param int $pageSize
	 *        	页面大小
	 * @return CLIST
	 */
	public function getGroups($pageNo, $pageSize = 10) {
		$list = new CLIST ();
		$totalCount = $this->getTotalCount ();
		$totalPage = $totalCount / $pageSize + ($totalCount % $pageSize) ? 1 : 0;
		// 检验页面数
		if ($pageNo > $totalPage) {
			$pageNo = $totalPage;
		}
		$offset = ($pageNo - 1) * $pageSize;
		$query = 'select id, name, info, ts_created from groups limit :offset,:pageSize';
		// @formatter:off
		$param = array ('offset'=>$offset,'pageSize'=>$pageSize);
		$map[] = array ('offset','int');
		$map[] = array ('pageSize','int');
		// @formatter:on
		$resultSet = $this->_conn->select ( $query, $param, $map );
		for($i = 0; $i < count ( $resultSet ); ++ $i) {
			$map = new CMAP ( $resultSet [$i] );
			$list->append ( $map );
		}
		
		return $list;
	}
}

